<?php

namespace App\Http\Controllers\Admin\Coupon;

use App\Http\Controllers\Controller;
use App\Models\StoreCouponIssue;
use App\Models\StoreCouponUser;
use App\Traits\Controllers\AdminTraits;
use App\Transformers\StoreCoupon\StoreCouponIssueProductTransformer;
use App\Transformers\StoreCoupon\StoreCouponIssueTransformer;
use App\Transformers\StoreCoupon\StoreCouponUserTransformer;
use App\Transformers\User\UserDetailsTransformer;
use Illuminate\Http\Request;

class CouponController extends Controller
{
    //使用增删改查等方法
    use AdminTraits;

    //优惠券相关的模型
    public $model = StoreCouponIssue::class;

    //优惠券相关的 Transformer
    public $transformer = StoreCouponIssueTransformer::class;

    //表单验证
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
                return [
                    'title' => 'required|max:50',  //验证优惠券的名称
                    'type' => [function ($attribute, $value, $fail) use ($request) { //使用闭包函数获取表单传来的值
                        if ($value == 'receives_two') {   //判断传来的类型是否是品类券
                            if ($request->category_id == null) {  //当为品类券的时候判断是否选择品类
                                $fail('请选择品类');  //当品类为空直接进行提示返回
                            }
                        }
                        if ($value == 'receives_three') {  //判断选择的是否是商品券
                            if ($request->product_id == null) {  //当为商品券的时候 判断是否选择商品
                                $fail('请选择商品');  //当没有选择商品的时候进行提示返回
                            }
                        }
                    }],
                    'coupon_price' => 'required|integer|min:1',   //优惠券的面值
                    'threshold' => [function ($attribute, $value, $fail) use ($request) {  //使用门槛 0为无门槛 1为门槛
                        if ($value == 1) {
                            if ($request->use_min_price == 0) {
                                $fail('优惠券最低消费不能小于0');
                            }
                        }
                    }],
                    'usagetime' => [function ($attribute, $value, $fail) use ($request) {  //使用时间 0为天数 1为时间段
                        if ($value == 0) {
                            if ($request->coupon_time < 1) {
                                $fail('有效期不能小于1天');
                            }
                        } else {
                            if ($request->start_use_time == null && $request->end_use_time == null) {
                                $fail('时间段不能为空');
                            }
                        }
                    }],
                    'collectiontime' => [function ($attribute, $value, $fail) use ($request) { //领取时间
                        if ($value == 0) {
                            if ($request->start_time == null && $request->end_time == null) {
                                $fail('领取的时间段不能为空');
                            }
                        }
                    }],
                    'is_permanent' => [function ($attribute, $value, $fail) use ($request) {  //是否限量
                        if ($value == 0) {
                            if ($request->limitquantity <= 0) {
                                $fail('优惠券发布数量不能为0');
                            }
                        }
                    }]
                ];
                break;
            case 'PUT':
                return [];
                break;
            case 'PATCH':
                return [];
                break;
        }
        return [];
    }

    // 验证错误返回的信息
    public function ruleMessages(): array
    {
        return [
            'title.required' => '优惠券名称不能为空',
            'title.max' => '优惠券最大不能超过50个字符',
            'coupon_price.min' => '优惠券面值不能小于0'
        ];
    }

    //添加用户优惠券的方法
    public function store(Request $request)
    {
        $this->validateData($request);
//        if ($request->usagetime == 1) {  // 转换时间字符串
//            $request->start_use_time = Carbon::parse($request->input('start_use_time'))->toDateTimeString();
//                               $time = Carbon::parse($data['created_at'])->toDateTimeString();//转换时间格式
//            $request->end_use_time = Carbon::parse($request->input('end_use_time'))->toDateTimeString();
//        }
//        dd($request->start_use_time);
        // 保存创建人
        $request->offsetSet('system_admin_id', $this->admin_user('id'));
        $model = $this->getQuery()->fill($request->all());
        $model->save();
        return $this->response->created();
    }

    //显示优惠券详情的方法
    public function show($id)
    {
        $show = $this->getQuery()
            ->where('id',$id)
            ->with('product:id,image')
            ->first();
        if ($this->policy) {
            $this->authorize('show', $show);
        }
        return $this->response->item($show, new StoreCouponIssueProductTransformer());
    }

    //查看单个优惠券的领取记录
    public function receive($id)
    {
        $user = $this->getQuery()
            ->find($id)    //使用当前模型查询到传来的优惠券的信息
            ->users()    //并使用查询到的优惠券的信息的模型中的关联用户的方法查询到所有关联到的用户信息的模型
            ->with('couponissueuser:user_id,created_at')   //再使用用户模型中的关联到的中间表中的信息拿去到 创建的时间 注意必须加上要查询的id
            ->paginate($this->getPageLimit());  //进行分页返回
        return $this->response->paginator($user, new UserDetailsTransformer());
    }

    //返回用户领取记录列表的方法
    public function usercollection()
    {
        $coupon = StoreCouponUser::query()
            ->pimp()  //进行查询
            ->with('user:id,phone,real_name,nickname')
            ->paginate($this->getPageLimit());
        return $this->response->paginator($coupon, new StoreCouponUserTransformer());
    }

    //更改优惠券的状态
    public function status($id, Request $request)
    {
        //获取到要修改的模型
        $data = $this->getQuery()->find($id);
        //将状态改为要表单传来的状态
        $data->status = $request->input('status');
        //进行更改后的保存
        $data->save();
        //返回添加后的状态
        return $this->response->noContent();
    }
}
